<?php

if (!isset($_REQUEST['tariff']) || !is_numeric($_REQUEST['tariff'])) {
    error_obj::setError(new error_obj("bad request tariff", __FILE__, 'line 4 -> redirect'), UNEXCEPTED);
    base_obj::redirect(ROOT);
}

$tariffId = (int) $_REQUEST['tariff'];

$tariff_obj = new tariff_obj();
$record_obj = new record_obj();

if (isset($_REQUEST['action'])){
    switch ($_REQUEST['action']) {
        case 'edit_tariff':
            $tariff = $_REQUEST['tariff_data'];
            $result = $tariff_obj->editTariff($tariff, $tariffId);
            if (error_obj::isError($result)){
                error_obj::setError($result, UNEXCEPTED);
            } else {
                base_obj::redirect(ROOT . '?input=list_of_tariffs');
            }
            
            break;
    }
}

$tariff_data = $tariff_obj->getTariffById($tariffId);

if (error_obj::isError($tariff_data)) {
    error_obj::setError($tariff_data, UNEXCEPTED);
} elseif (empty($tariff_data)) {
    error_obj::setError(new error_obj("empty tariff", __FILE__, 'get tariff data'), EMPTY_tariff);
} else {
    
    $tariff['types_of_record'] = $record_obj->getRecordTypes();

    $GLOBALS['template_data'] = $tariff;
    $GLOBALS['template_data']['tariff_data'] = $tariff_data;
    
}
?>
